﻿using ZQCY.Watchmen.Extensions.Extensions;
using ZQCY.Watchmen.Extensions.Filter;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using HBKY.Shop.Common.Helper;
using HBKY.Shop.Extensions.ServiceExtensions;
using HBKY.Shop.Extensions.Filter;
using Newtonsoft.Json;
using HBKY.Shop.Common.Constant;
using ZQCY.Appointment.Extensions.Extensions;

namespace HBKY.Shop.Extensions.CoreBuilder
{
    public class ServiceBuilder : ICoreServiceBuilder
    {
        private readonly IServiceCollection _services;
        private readonly IConfiguration _configuration;
        public ServiceBuilder(IServiceCollection services, IConfiguration configuration)
        {
            _services = services;
            _configuration = configuration;
        }

        /// <summary>
        /// 注册AutoMapper
        /// </summary>
        public void AddAutoMapper()
        {
            _services.AddAutoMapperSetup();
        }

        /// <summary>
        /// 缓存 
        /// </summary>
        public void AddCache()
        {
            _services.AddRedisCacheSetup();
        }

        //跨域配置
        public void AddCors()
        {
            _services.AddCorsSetup();
        }

        public void AddDbcontext()
        {
        }

        public void AddHttpClient()
        {
            _services.AddHttpClient();
        }

        /// <summary>
        /// 添加上下文信息
        /// </summary>
        public void AddHttpContext()
        {

        }

        /// <summary>
        /// 添加日志模板
        /// </summary>
        public void AddNlogLayoutRenderers()
        {
            LayoutRenderersExt.RegisterDefinition();
        }

        /// <summary>
        /// jwt 授权认证
        /// </summary>
        public void AddJwtAuth()
        {
            return;
        }

        public void AddLog()
        {
            
        }

        public void AddMvcExtensions()
        {
            _services.AddSingleton(new Appsettings());

            _services.AddControllers(options =>
            {
                options.Filters.Add<ExceptionHandleFilter>();//全局注册
                options.Filters.Add<CustomResultFilter>();      //参数校验
            })
            //全局配置Json序列化处理
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;//忽略循环引用
                options.SerializerSettings.DateFormatString = DateTimeFormatConst.yyyy_MM_ddHHmmss;//设置时间格式
            });
        }
        public void AddSqlSugar()
        {
          _services.AddSqlsugarSetup();
        }

        /// <summary>
        /// 添加Swagger
        /// </summary>
        public void AddSwaggerGenerator()
        {
            _services.AddSwaggerSetup();
        }

        /// <summary>
        /// 添加定时作业
        /// </summary>
        public void AddTaskJob()
        {
            _services.AddJobSetup();
        }

        /// <summary>
        /// KafkaConsumer
        /// </summary>
        public void AddConsumer()
        {
        }

        /// <summary>
        /// KafkaProducer
        /// </summary>
        public void AddProducer()
        {
        }

        public void AddRedisInitMqSetup()
        {
        }

        /// <summary>
        /// 添加存储
        /// </summary>
        public void AddStorage()
        {
           
        }
    }
}
